package com.yonyou.biz.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;

import com.intellij.openapi.diagnostic.Logger;
import com.yonyou.biz.vo.MyPluginState;
import com.yonyou.utils.MyPluginStateComponent;
import git4idea.repo.GitRemote;

import git4idea.*;
import git4idea.branch.GitBranchUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.gitlab4j.api.Constants;
import org.gitlab4j.api.GitLabApi;


import git4idea.repo.GitRepository;
import org.gitlab4j.api.IssuesApi;
import org.gitlab4j.api.MergeRequestApi;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.IssueFilter;
import org.gitlab4j.api.models.Project;


import javax.swing.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class MergeAction extends AnAction {

    private static final Logger LOG = Logger.getInstance(MergeAction.class);
    @Override
    public void actionPerformed(AnActionEvent e) {

        com.intellij.openapi.project.Project idea_project = e.getProject();
        try {
            GitRepository  repository = GitBranchUtil.getCurrentRepository(idea_project);
            Collection<GitRemote> remotes = repository.getRemotes();
            if (remotes.isEmpty()) {
                com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("异常","未找到文件");
                return;
            }
            // Assuming the first remote is the one we want, get its URL
            String remoteUrl = remotes.iterator().next().getUrls().get(0);

            MyPluginState state = MyPluginStateComponent.getInstance().getState();
            if(StringUtils.isEmpty(state.getGit_token())){
                throw new RuntimeException("GIT秘钥为空、请先去设置秘钥！");
            }
            GitLabApi gitLabApi = new GitLabApi(remoteUrl.substring(0,remoteUrl.indexOf("com")+4),state.getGit_token());
            gitLabApi.getProjectApi().getProjects();
            com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("提示","检查通过！");
        } catch (Exception es) {
            com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showErrDialog("提示","检查异常！"+es.getMessage());
        }

    }

}
